package com.osce.score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.score.ScoreExModel;
import com.osce.common.utils.DateUtils;
import com.osce.interfaces.score.IScoreExModelPlaceService;
import com.osce.interfaces.score.IScoreExModelService;
import com.osce.interfaces.score.IScoreExModelTimeService;
import com.osce.score.mapper.ScoreExModelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 考试模版表Service业务层处理
 *
 * @author zzx
 * @since 2023-08-22
 */
@Service
public class ScoreExModelServiceImpl extends ServiceImpl<ScoreExModelMapper, ScoreExModel> implements IScoreExModelService {

    @Resource
    private IScoreExModelPlaceService placeService;
    @Resource
    private IScoreExModelTimeService timeService;


    /**
     * 查询考试模版表
     *
     * @param id 考试模版表主键
     * @return 考试模版表
     */
    @Override
    public ScoreExModel selectScoreExModelById(Long id) {
        return baseMapper.selectScoreExModelById(id);
    }

    /**
     * 查询考试模版表列表
     *
     * @param scoreExModel 考试模版表
     * @return 考试模版表
     */
    @Override
    public List<ScoreExModel> selectScoreExModelList(ScoreExModel scoreExModel) {
        return baseMapper.selectScoreExModelList(scoreExModel);
    }

    /**
     * 新增考试模版表
     *
     * @param scoreExModel 考试模版表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScoreExModel(ScoreExModel scoreExModel) {
        scoreExModel.setCreateTime(DateUtils.getNowDate());
        int rows = baseMapper.insertScoreExModel(scoreExModel);
        timeService.batchScoreExModelTime(scoreExModel.getScoreExModelTimeList(), scoreExModel.getId());
        placeService.batchScoreExModelPlace(scoreExModel.getScoreExModelPlaceList(), scoreExModel.getId());
        return rows;
    }

    /**
     * 修改考试模版表
     *
     * @param scoreExModel 考试模版表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScoreExModel(ScoreExModel scoreExModel) {
        timeService.deleteScoreExModelTimeByModelId(scoreExModel.getId());
        timeService.batchScoreExModelTime(scoreExModel.getScoreExModelTimeList(), scoreExModel.getId());
        placeService.deleteScoreExModelPlaceByModelId(scoreExModel.getId());
        placeService.batchScoreExModelPlace(scoreExModel.getScoreExModelPlaceList(), scoreExModel.getId());
        return baseMapper.updateScoreExModel(scoreExModel);
    }

    /**
     * 批量删除考试模版表
     *
     * @param ids 需要删除的考试模版表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScoreExModelByIds(Long[] ids) {
        timeService.deleteScoreExModelTimeByModelIds(ids);
        placeService.deleteScoreExModelPlaceByModelIds(ids);
        return baseMapper.deleteScoreExModelByIds(ids);
    }

    /**
     * 删除考试模版表信息
     *
     * @param id 考试模版表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScoreExModelById(Long id) {
        timeService.deleteScoreExModelTimeByModelId(id);
        placeService.deleteScoreExModelPlaceByModelId(id);
        return baseMapper.deleteScoreExModelById(id);
    }
}
